<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>欢迎使用</h2>
        <p>数据标注大屏后台管理系统</p>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            autocomplete="off"
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </el-button>
        </el-form-item>
        
        <div class="login-footer">
          <el-button type="text" @click="goToRegister">没有账号？去注册</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import 'element-ui/lib/theme-chalk/index.css';
import { login } from '@/utils/auth';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          login(this.loginForm.username, this.loginForm.password)
            .then(res => {
              this.loading = false;
              if (res.code === 200) {
                // 保存用户信息
                localStorage.setItem('admin_user', JSON.stringify(res.data.user));
                this.$message.success('登录成功');
                // 跳转到首页或重定向页面
                this.$router.push({ path: this.redirect || '/admin' });
              } else {
                this.$message.error(res.message || '登录失败');
              }
            }).catch(error => {
              this.loading = false;
              // 统一错误处理
              this.handleError(error);
            });
        }
      });
    },
    
    // 统一错误处理
    handleError(error) {
      if (error.response) {
        switch (error.response.status) {
          case 401:
            this.$message.error('用户名或密码错误');
            break;
          case 403:
            this.$message.error('权限不足');
            break;
          default:
            this.$message.error('系统错误');
        }
      } else {
        // 网络错误
        this.$message.error('网络连接失败');
      }
      console.error('登录错误:', error);
    },
    
    // 去注册
    goToRegister() {
      this.$router.push('/admin/register');
    }
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
    background-size: 50px 50px;
    animation: move 20s linear infinite;
  }

  @keyframes move {
    0% {
      transform: translate(0, 0);
    }
    100% {
      transform: translate(50px, 50px);
    }
  }
}

.login-box {
  width: 400px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  position: relative;
  z-index: 1;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;

  h2 {
    margin: 0 0 10px;
    font-size: 28px;
    color: #333;
    font-weight: 600;
  }

  p {
    margin: 0;
    font-size: 14px;
    color: #999;
  }
}

.login-form {
  :deep(.el-input__inner) {
    height: 45px;
    line-height: 45px;
    border-radius: 6px;
  }

  :deep(.el-input__prefix) {
    line-height: 45px;
  }
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 6px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  
  &:hover {
    background: linear-gradient(135deg, #5568d3 0%, #6a4093 100%);
  }
}

.login-footer {
  text-align: center;
  margin-top: 20px;

  p {
    margin: 0;
    font-size: 12px;
    color: #999;
  }
}
</style>
